//
//  bendimoreViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/11/17.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "titlexqViewController.h"
#import "picxqViewController.h"
#import "bendimoreViewController.h"
#import "titleTableViewCell.h"
@interface bendimoreViewController ()

@end

@implementation bendimoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //12&numPerPage=
        self.title = _titlename;
     _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
        if (weakSelf.isSearching) {
//     [self loadSearchMore];
}
     else {
     [weakSelf loadMore];
     }
        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:@"没有更多了"];
            
        }
        
    }];
}
-(void)loadData{

//http://192.168.1.181:8080/web/article/articleClassId?article.ClassId=
NSString *string = [[NSString alloc]initWithFormat:@"%@%@&pageNum=%d",kbendimore,_id,_currentPage];
[httpTool ZBPostNetDataWith:string withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
_dataArray =   [NSMutableArray array];
  NSArray *array = dictionary[@"list"];
    for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
  self.totalCounts =[dictionary[@"totalRow"] intValue];

[_tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
         [self.tableView headerEndRefreshing];

 }];


}
-(void)loadMore{


NSString *string = [[NSString alloc]initWithFormat:@"%@%@&pageNum=%d",kbendimore,_id,_currentPage];
[httpTool ZBPostNetDataWith:string withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 
 NSArray *array = dictionary[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}        self.currentPage++;

  self.totalCounts =[dictionary[@"totalRow"] intValue];
        [self.tableView footerEndRefreshing];
[_tableView reloadData];
    
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
        [self.tableView footerEndRefreshing];

 }];


}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 NSDictionary *dic = self.dataArray[indexPath.row];


 if ([dic[@"defaultPicUrl"]isEqual:[NSNull null]]) {
     titlexqViewController *vc = [[titlexqViewController alloc]init];
    vc.id = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}else{

 picxqViewController *vc = [[picxqViewController alloc]init];
    vc.id = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}
};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"Celltitle";

    titleTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
        cell=[[titleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 NSDictionary *dic = self.dataArray[indexPath.row];

  CGFloat titleSize = [OAToolBox sizebyText:dic[@"title"] font:12 minsWidth:10+120];
if (titleSize<60) {
    return 60+20;
}else{
return titleSize+20;

}


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
